package cn.ithers.blog.dao;

import cn.ithers.blog.pojo.Label;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.jmx.export.annotation.ManagedOperation;

import javax.transaction.Transactional;

/**
 * @Author: zhanghuan
 * @date: 2020/8/29 10:06
 * @description:
 */
public interface LabelDao extends JpaRepository<Label, String>, JpaSpecificationExecutor<Label> {

    @Modifying
    int deleteOneById(String id);

    /**
     * 自定义
     *
     * @param id
     * @return
     */
    @Transactional
    @Modifying
    @Query(value = "DELETE FROM tb_labels WHERE id = ?", nativeQuery = true)
    int customDeleteLabelById(String id);

    /**
     * 根据id查找一个标签
     *
     * @param id
     * @return
     */
    Label findOneById(String id);

    Label findOneByName(String name);

    @Modifying
    @Query(nativeQuery = true, value = "UPDATE tb_labels SET count = count + 1 WHERE name = ?")
    int updateCountByName(String labelName);
}
